#include "ServerFlashBang.h"

#define TIMETOWAIT 180;

bool ServerFlashBang::checkShieldForHit(ServerRiotShield* shield){
	double lx, ly, hx, hy;
	shield->getBoundingBox(&lx, &ly, &hx, &hy);
	double mlx = myPos.x -20;
	double mly = myPos.y - 20;
	double mhx = myPos.x + 20;
	double mhy = myPos.y + 20;
	if(lx <= mhx && mlx <= hx && ly <= mhy && mly <= hy){
		return 1;
	}
	return 0;
}

ServerFlashBang::ServerFlashBang(ServerScoreCard* score1, ServerRiotShield* player1, ServerScoreCard* score2, ServerRiotShield* player2, NetworkHandler *toSend, int target){
	handler = toSend;
	tgt = target;
	dir = -1;
	myPos.z = -1 * dir * POLICEZ;
	randomizeVelocity(&myPos.vx, &myPos.vy);
	myPos.vz = dir * FBINITV;
	s1 = score1;
	s2 = score2;
	p1 = player1;
	p2 = player2;
	gameOver = 0;
	timeToWait = 0;
}

void ServerFlashBang::updateState(){
	bool playWall = 0;
	bool playShield = 0;
	bool playOhMyGod = 0;
	if(gameOver){
		return;
	}
	if(timeToWait > 0){
		timeToWait--;
		if(timeToWait <= 0){
			dir = -dir;
			myPos.z = -1 * dir * POLICEZ;
			myPos.x = 0;
			myPos.y = 0;
			myPos.vz = dir * FBINITV;
			randomizeVelocity(&(myPos.vx), &(myPos.vy));
		}
		return;
	}
	//reconcile physics with myself
	//
	//
	//
	if(true){
		double grav = -2;
		myPos.accelerate(0,grav,0);
		myPos.updatePosition();
		if(myPos.x > (MAXX - 20) && myPos.vx > 0){
			myPos.x = MAXX-20;
			myPos.vx = -(myPos.vx);
			playWall = 1;
		}
		if(myPos.x < (MINX + 20) && myPos.vx < 0){
			myPos.x = MINX+20;
			myPos.vx = -(myPos.vx);
			playWall = 1;
		}
		if(myPos.y > (MAXY - 20) && myPos.vy > 0){
			myPos.y = MAXY-20;
			myPos.vy = -(myPos.vy);
			playWall = 1;
		}
		if(myPos.y < (MINY + 20) && myPos.vy < 0){
			myPos.y = MINY+40;
			myPos.vy = -(myPos.vy);
			playWall = 1;
		}
		if(myPos.z > (POLICEZ-20) && myPos.vz > 0){
			if(checkShieldForHit(p1)){
				myPos.vz = -(myPos.vz + 5);
				playShield = 1;
			}
			else{
				playOhMyGod = 1;
				s2->addScore(1);
				if(s2->getScore()>=MAXSCORE){
					gameOver = 1;
				}
				timeToWait = TIMETOWAIT;
			}
		}
		if(myPos.z < (INMATESZ+20) && myPos.vz < 0){
			if(checkShieldForHit(p2)){
				myPos.vz = -(myPos.vz + 5);
				playShield = 1;
			}
			else{
				playOhMyGod = 1;
				s1->addScore(1);
				if(s1->getScore()>=MAXSCORE){
					gameOver = 1;
				}
				timeToWait = TIMETOWAIT;
			}
		}
	}
	//send pos,velocity,pWall,pShield,pOhGod,pPin,gameOver
	buffer[0] = (char)playWall;
	buffer[1] = (char)playShield;
	buffer[2] = (char)playOhMyGod;
	buffer[3] = (char)gameOver;
	//send data over the network
	int bufferLoc = 4;
	bufferLoc += conv.writeConvertedDouble(myPos.x, (buffer + bufferLoc));
	bufferLoc += conv.writeConvertedDouble(myPos.y, (buffer + bufferLoc));
	bufferLoc += conv.writeConvertedDouble(myPos.z, (buffer + bufferLoc));
	bufferLoc += conv.writeConvertedDouble(myPos.vx, (buffer + bufferLoc));
	bufferLoc += conv.writeConvertedDouble(myPos.vy, (buffer + bufferLoc));
	bufferLoc += conv.writeConvertedDouble(myPos.vz, (buffer + bufferLoc));
	//actually send
	handler->addBroadcastMessage(buffer, bufferLoc, tgt);
}

void ServerFlashBang::alertToInput( const InputSet& input ){}

void ServerFlashBang::randomizeVelocity(double* xv, double* yv){
	*xv = FBINITV * ((2*((1.0 * rand()) / RAND_MAX))-1);
	*yv = (FBINITV * ((2*((1.0 * rand()) / RAND_MAX))-1));
}

